package com.luyu.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.luyu.datapermission.BaseDataPermissionHandler;
import com.luyu.datapermission.FieldDataPermissionSqlImpl;
import com.luyu.tenant.AuthTenantLineHandler;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MpConfig {

    @Resource
    private AuthTenantLineHandler authTenantLineHandler;
    @Resource
    private FieldDataPermissionSqlImpl fieldDataPermissionSql;

    /**
     * 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租户插件
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(authTenantLineHandler);
        interceptor.addInnerInterceptor(tenantLineInnerInterceptor);

        //数据权限插件
        DataPermissionInterceptor permissionInterceptor =
                new DataPermissionInterceptor(new BaseDataPermissionHandler(fieldDataPermissionSql));
        interceptor.addInnerInterceptor(permissionInterceptor);


        // 分页插件一定最后加
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}
